﻿using System.Security.AccessControl;

namespace System.IO
{
    internal class InMemoryFileAccessor : FileAccessorBase
    {
        private readonly InMemoryFileSystemStore _store;

        public InMemoryFileAccessor(InMemoryFileSystemStore store)
        {
            _store = store;
        }

        public override void Copy(string sourceFileName, string destFileName, bool overwrite)
        {
            _store.CopyFile(sourceFileName, destFileName, overwrite);
        }

        public override Stream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)
        {
            return _store.CreateFile(path, bufferSize, options, fileSecurity);
        }

        public override void Delete(string path)
        {
            _store.Delete(path);
        }

        public override void Decrypt(string path)
        {
            throw new NotSupportedException();
        }

        public override void Encrypt(string path)
        {
            throw new NotSupportedException();
        }

        public override bool Exists(string path)
        {
            var info = _store.GetFileInfo(path);
            return info.Exists;
        }

        public override Stream Open(string path, FileMode mode, FileAccess access, FileShare share)
        {
            return _store.OpenFile(path, mode, access, share);
        }

        public override void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
        {
            var info = _store.GetFileInfo(path);
            info.CreationTimeUtc = creationTimeUtc;
        }

        public override DateTime GetCreationTimeUtc(string path)
        {
            var info = _store.GetFileInfo(path);
            return info.CreationTimeUtc;
        }

        public override void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
        {
            var info = _store.GetFileInfo(path);
            info.LastAccessTimeUtc = lastAccessTimeUtc;
        }

        public override DateTime GetLastAccessTimeUtc(string path)
        {
            var info = _store.GetFileInfo(path);
            return info.LastAccessTimeUtc;
        }

        public override void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
        {
            var info = _store.GetFileInfo(path);
            info.LastWriteTimeUtc = lastWriteTimeUtc;
        }

        public override DateTime GetLastWriteTimeUtc(string path)
        {
            var info = _store.GetFileInfo(path);
            return info.LastWriteTimeUtc;
        }

        public override FileAttributes GetAttributes(string path)
        {
            var info = _store.GetFileInfo(path);
            return info.Attributes;
        }

        public override void SetAttributes(string path, FileAttributes fileAttributes)
        {
            var info = _store.GetFileInfo(path);
            info.Attributes = fileAttributes;
        }

        public override FileSecurity GetAccessControl(string path)
        {
            throw new NotImplementedException();
        }

        public override FileSecurity GetAccessControl(string path, AccessControlSections includeSections)
        {
            throw new NotImplementedException();
        }

        public override void SetAccessControl(string path, FileSecurity fileSecurity)
        {
            throw new NotImplementedException();
        }

        public override void Move(string sourceFileName, string destFileName)
        {
            _store.MoveFileTo(sourceFileName, destFileName);
        }

        public override void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName,
                                     bool ignoreMetadataErrors)
        {
            _store.Replace(sourceFileName, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
        }
    }
}